<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/10
 * Time: 10:38
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class AssignmentModel
{
    const TABLE = 'assignment';

    /**
     * 施工项分页列表
     * @param array $param
     * @return mixed
     */
    public function lists(array $param)
    {
        $result =  DB::table(self::TABLE)
            ->leftJoin('unit as u','u.id','=',self::TABLE.'.unitId')
            ->leftJoin('assignment_label as al','al.id','=',self::TABLE.'.labelId')
            ->where(function ($query) use ($param){
                if (isset($param['professionId'])){
                    $query->where('professionId',$param['professionId']);
                }
                if (isset($param['search']) && !is_null($param['search'])){
                    $query->where(self::TABLE.'.name','like','%'.$param['search'].'%');
                }
            })
            ->orderBy('order','desc');
        if(!empty($param['length']) && !empty($param['page'])){
            $limit = config('yucheng.limit');
            $start = 0;
            if (isset($param['length']) && !is_null($param['length'])) {
                $limit = $param['length'];
            }
            if (isset($param['page']) && !is_null($param['page'])) {
                $start = ($param['page']-1)*$limit;
            }
            $result = $result->offset($start)->limit($limit);
        }

        return $result->select(self::TABLE.'.*','u.name as unitName','al.name as labelName')
            ->get()->toArray();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function countLists(array $param)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($param){
                if (isset($param['professionId'])){
                    $query->where('professionId',$param['professionId']);
                }
                if (isset($param['search'])){
                    $query->where('name','like','%'.$param['search'].'%');
                }
            })
            ->count();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function selectLists(array $param)
    {
        return DB::table(self::TABLE)
            ->leftJoin('unit as u','u.id','=',self::TABLE.'.unitId')
            ->where(function ($query) use ($param){
                if (isset($param['professionId'])){
                    $query->where('professionId',$param['professionId']);
                }
            })
            ->orderBy('order','desc')
            ->select(self::TABLE.'.*','u.name as unitName')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return array
     */
    public function info($data = [])
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * 检查是否有重复的施工项
     * @param array $check
     * @param $id
     * @return array
     */
    public function checkRepeat(array $check, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($check)
            ->where(function ($query) use ($id){
                if ($id != 0){
                    $query->where('id','!=',$id);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where ,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }
}